package com.hui.security.sys.service.impl;

import com.hui.security.sys.entity.DataRule;
import com.hui.security.sys.exception.DataRuleException;
import com.hui.security.sys.exception.ExceptionEnum;
import com.hui.security.sys.mapper.DataRuleMapper;
import com.hui.security.sys.service.DataRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <b><code>DataRuleServiceImpl</code></b>
 * <p/>
 * Description:
 * <p/>
 * <b>Creation Time:</b> 2019/1/5 0:27.
 *
 * @author HuWeihui
 */
@Service
public class DataRuleServiceImpl implements DataRuleService {

    @Autowired
    private DataRuleMapper dataRuleMapper;

    /**
     * 新增数据规则
     *
     * @param dataRule
     * @return
     */
    @Override
    public DataRule add(DataRule dataRule) {
        DataRule repeatRule = dataRuleMapper.selectByRuleName(dataRule.getRuleName());
        if (null == repeatRule) {
            throw new DataRuleException(ExceptionEnum.DATA_RULE_ERROR_REPEAT_RULE);
        }
        dataRuleMapper.insertSelective(dataRule);
        return dataRule;
    }

    /**
     * 删除数据规则
     *
     * @param ruleId
     */
    @Override
    public void delete(String ruleId) {
        dataRuleMapper.deleteByPrimaryKey(ruleId);
    }

    /**
     * 更新数据规则
     *
     * @param dataRule
     */
    @Override
    public void update(DataRule dataRule) {
        dataRuleMapper.updateByPrimaryKey(dataRule);
    }

    /**
     * 查询数据规则
     *
     * @param ruleId
     * @return
     */
    @Override
    public DataRule get(String ruleId) {
        return dataRuleMapper.selectByPrimaryKey(ruleId);
    }

    /**
     * 查询所有数据规则.
     *
     * @return the list
     * @author : Hu weihui
     * @since nile -cmszbs-szcst 0.1.0
     */
    @Override
    public List<DataRule> listAll() {
        return dataRuleMapper.selectAll();
    }
}
